capture log close

// Set the directory to your own. Here is an example
cd "~/Dropbox/2018 Coalitions/Documentation/Replication files/"

// Start log. Here is an example
log using aq-analyze-table-dv-quota-predict, replace text

// program: Stata 12.1
// task:  Calculate predicted probabilities for Table 5, using Table 3, Model 3
// project: Kang and Tripp 2018 Perspectives on Politics
// author: Alice J. Kang

version 12.1
clear all
set linesize 80
set more off

// Bring in data
use KangTrippReplication.dta

// #1: Make entry variable due to different independence years
*Late entry: Eritrea in 1993, Namibia in 1990, South Sudan in 2011
sort country year
gen enter=1989
by country: replace enter=1993 if country=="Eritrea"
by country: replace enter=1990 if country=="Namibia"
label variable enter "1989 or year of independence if after"

// #2: Make begin variable
* Begin risk at 1989
gen begin=1989

// #3: Set
stset year, id(country) failure(quota==1) enter (time enter-1) ///
	origin(time begin-1)

// #4: Make duration dependence variables
rename _t t
gen pluralt=plural_lead*t

// #5: Figure out what values to use
sum country year _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 ///
	plural_lead l2gnipcatlasl musmaj coalition t if lbeijingpc!=. ///
	& groups2!=. & plural_lead!=. & _d!=.
* For categorical values, set at median.
sum polity2 if groups2!=. & plural_lead!=. & _d!=., detail
sum groups2 if groups2!=. & plural_lead!=. & _d!=., detail
sum lbeijingpc if lbeijingpc!=. & groups2!=. & plural_lead!=. & _d!=., detail
* For values that don't vary across countries and vary over time, use year 2000 values
sum iwm_cum2 if groups2!=. & plural_lead!=. & _d!=. & year==2000
sum year t if year==2000
* For lbeijingpc, use 50%, 90%: -1.00, .37
* Look at untransformed log Beijing pc
gen junkb=exp(lbeijingpc) 
sum junkb if groups2!=. & plural_lead!=. & _d!=., detail
* For Beijingpc, 50%, 90%: .37, 1.45
* di iwm_cum2*lbeijingpc
* For values that vary over country and time, use mean over the total sample

// #6: Predict using Table 3, Column 3: Must follow this order!
cloglog _d iwm_cum2 lbeijingpc iwm_cum2xbpc ldacpcle conf_cat23 polity2 groups2 plural_lead ///
	l2gnipcatlasl musmaj coalition t pluralt, cluster(country)
* Set values: coalition is 0 and conf_cat23 is 0
replace coalition=0
replace conf_cat23=0
replace polity2=0
replace groups2=5
replace t=12
replace iwm_cum2=7.75
replace lbeijingpc=-1.00
replace iwm_cum2xbpc=-7.75
replace ldacpcle=3.00
replace l2gnipcatlasl=6.52
replace plural_lead=0
replace musmaj=0
replace pluralt=0
* Predict
predict haz, pr
gen junk=ln(1-haz)
by country, sort: gen lnsurv=sum(junk)
gen surv=exp(lnsurv)
sum surv, detail 
* Get median number (50%)
di 1-.9583
drop haz junk lnsurv surv

* Set values: coalition is 1 and conf_cat23 is 0
replace coalition=1
replace conf_cat23=0
replace polity2=0
replace groups2=5
replace t=12
replace iwm_cum2=7.75
replace lbeijingpc=-1.00
replace iwm_cum2xbpc=-7.75
replace ldacpcle=3.00
replace l2gnipcatlasl=6.52
replace plural_lead=0
replace musmaj=0
replace pluralt=0
* Predict
predict haz, pr
gen junk=ln(1-haz)
by country, sort: gen lnsurv=sum(junk)
gen surv=exp(lnsurv)
sum surv, detail 
* Get median number (50%)
di 1-.6264
drop haz junk lnsurv surv

* Set values: coalition is 0 and conf_cat23 is 1
replace coalition=0
replace conf_cat23=1
replace polity2=0
replace groups2=5
replace t=12
replace iwm_cum2=7.75
replace lbeijingpc=-1.00
replace iwm_cum2xbpc=-7.75
replace ldacpcle=3.00
replace l2gnipcatlasl=6.52
replace plural_lead=0
replace musmaj=0
replace pluralt=0
* Predict
predict haz, pr
gen junk=ln(1-haz)
by country, sort: gen lnsurv=sum(junk)
gen surv=exp(lnsurv)
sum surv, detail 
* Get median number (50%) 
di 1-.5331
drop haz junk lnsurv surv
 
* Set values: coalition is 1 and conf_cat23 is 1
replace coalition=1
replace conf_cat23=1
replace polity2=0
replace groups2=5
replace t=12
replace iwm_cum2=7.75
replace lbeijingpc=-1.00
replace iwm_cum2xbpc=-7.75
replace ldacpcle=3.00
replace l2gnipcatlasl=6.52
replace plural_lead=0
replace musmaj=0
replace pluralt=0
* Predict
predict haz, pr
gen junk=ln(1-haz)
by country, sort: gen lnsurv=sum(junk)
gen surv=exp(lnsurv)
sum surv, detail 
* Get median number (50%) 
di 1-.001
drop haz junk lnsurv surv
 
* Set values: coalition is 0 and conf_cat23 is 0, Beijing 90%
* Will need to also change iwm_cum2xbpc
replace coalition=0
replace conf_cat23=0
replace polity2=0
replace groups2=5
replace t=12
replace iwm_cum2=7.75
replace lbeijingpc=.37
di 7.75*.37
replace iwm_cum2xbpc=2.8675
replace ldacpcle=3.00
replace l2gnipcatlasl=6.52
replace plural_lead=0
replace musmaj=0
replace pluralt=0
* Predict
predict haz, pr
gen junk=ln(1-haz)
by country, sort: gen lnsurv=sum(junk)
gen surv=exp(lnsurv)
sum surv, detail 
* Get median number (50%) 
di 1-.9430
drop haz junk lnsurv surv

* Set values: coalition is 0 and conf_cat23 is 0 and year is 2010
replace coalition=0
replace conf_cat23=0
replace polity2=0
replace groups2=5
replace t=22
replace iwm_cum2=7.75
replace lbeijingpc=-1.00
replace iwm_cum2xbpc=-7.75
replace ldacpcle=3.00
replace l2gnipcatlasl=6.52
replace plural_lead=0
replace musmaj=0
replace pluralt=0
* Predict
predict haz, pr
gen junk=ln(1-haz)
by country, sort: gen lnsurv=sum(junk)
gen surv=exp(lnsurv)
sum surv, detail 
* Get median number (50%)
di 1-.7165

log close
exit
